package com.wpw.controller;


import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wpw.domain.User2;
import com.wpw.domain.UserRepository;
import com.wpw.properties.CustomProperties;
import com.wpw.properties.MyProperties;




@RestController
public class RestControllerDemo {
	
	@Autowired
	private MyProperties myProperties;
	
	@Autowired
	private UserRepository userRepository;
	
	@Autowired
	private CustomProperties customProperties;
	
	@Autowired
	private RedisTemplate<Object, Object> redisTemplate;
	
	@RequestMapping("/hello")
    public String index() {
		System.out.println(myProperties.getTitle());
		System.out.println(myProperties.getDescription());
		System.out.println(customProperties.getTest());
        return "Hello World" + customProperties.getTest();
    }
	
	/**
	 * 缓存
	 * @Description：
	 * @author: wupengwei
	 * @date: 2017年12月29日 下午3:10:44
	 * @return
	 */
	@RequestMapping("/redisTest")
	public Object redisTest() {
		User2 user=new User2("aa", "aa", "aa@126.com", "aa","123");
		redisTemplate.opsForValue().set("aa", user);
		return redisTemplate.opsForValue().get("aa");
	}
	
	/**
	 * 缓存
	 * @Description：
	 * @author: wupengwei
	 * @date: 2017年12月29日 下午3:10:36
	 * @param userName
	 * @return
	 */
	@RequestMapping("/getUser")
	@Cacheable(value = "user", key = "#userName")//value好像可以随意配置，如果key为userName可以命中缓存，不会执行该方法
	public User2 getUser(String userName, Long id) {
	    User2 user=userRepository.findByUserName(userName);
	    return user;
	}
	
	/**
	 * redis session
	 * @Description：
	 * @author: wupengwei
	 * @date: 2017年12月29日 下午3:10:27
	 * @param session
	 * @return
	 */
	@RequestMapping("/uid")
    String uid(HttpSession session) {
        UUID uid = (UUID) session.getAttribute("uid");
        if (uid == null) {
            uid = UUID.randomUUID();
        }
        session.setAttribute("uid", uid);
        return session.getId();
    }
}